package com.unionServer.common;

import lombok.extern.slf4j.Slf4j;
import org.dom4j.Element;
import org.springframework.util.StringUtils;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;

/**
 * @description:
 * @author: wangxinal
 * @time: 2022/1/21 14:02
 */
@Slf4j
public class FieldUnpack {
    public static void AtmpinUnPackTLVTag(String data, String fieldName, HashMap<String, Object> map, Element curNode) throws UnsupportedEncodingException {
        log.info("unPackTLVTag 原始值：" + data);

        String name = "" ;
        String usage = "" ;
        String v_tag_hex = "" ;
        String v_tag_temp = "" ;
        String v_tag_temp1 = "" ;
        String v_length_hex = "" ;
        String tag = "" ;
        String leng = "" ;
        String value = "" ;

        if(null != data && data.length() != 0){
            while (data.length() > 0 ){
                v_tag_hex = data.substring(0,4);
                usage = toStringHex(v_tag_hex);
                data = data.substring(v_tag_hex.length());
                v_length_hex = data.substring(0,6);
                int len_dec_int = Integer.parseInt(toStringHex(v_length_hex));

                data = data.substring(v_length_hex.length());
                v_tag_temp = data.substring(0,len_dec_int*2);
                v_tag_temp1 = v_tag_temp ;
                //判断是定长还是TLV
                if(curNode.element(usage).attributeValue("mode") != null
                        && curNode.element(usage).attributeValue("mode").equals("TLV") ){
                    //标识符+长度+值

                    value = v_tag_temp;
                    name = fieldName + "_" + usage ;
                    log.info("field Name is : " + name + " field value is : " + value);
                    map.put(name, value);

                }
                else if(curNode.element(usage).attributeValue("mode") != null
                        && curNode.element(usage).attributeValue("mode").equals("STLV") ) {
                    //标识符+子域+子域长度+子域值+子域+子域长度+子域值+。。。


                    int i = 0;
                    for(;i<v_tag_temp.length();i++){
                        tag = v_tag_temp.substring(0,2);
                        v_tag_temp = v_tag_temp.substring(tag.length());
                        leng = v_tag_temp.substring(0,2);
                        v_tag_temp = v_tag_temp.substring(leng.length());
                        value = v_tag_temp.substring(0,
                                Integer.parseInt(leng,16)*2);
                        name = fieldName + "_" + usage + "_" + tag ;
                        log.info("field Name is : " + name + " field value is : " + value);
                        map.put(name, value);

                        if(value.length() + 4 < v_tag_temp.length() ){
                            v_tag_temp = replace(v_tag_temp,value,"",1);
                            if(v_tag_temp.length() < 6) {
                                v_tag_temp = "";
                            }
                        }else {
                            v_tag_temp = "";
                        }
                    }
                }

                data = replace(data,v_tag_temp1,"",1);
                if(data.length() < 10) {
                    data = "";
                }
            }
        }else {
            log.info("Field " + fieldName + "不存在");
        }
    }

    public static void StringLengthUnPack(String data, String fieldName, HashMap<String, Object> map, Element curNode) throws UnsupportedEncodingException {
        log.info("StringLengthUnPack 原始值：" + data);

        String name = "" ;
        String usage = "" ;
        String v_tag_temp = "" ;
        String v_tag_temp1 = "" ;
        String value = "" ;
        Iterator itr = curNode.elementIterator();

        while (itr.hasNext()){
            Element subNode = (Element)itr.next();
            int length = subNode.attributeValue("compressLength") == null
                    ? Integer.parseInt(subNode.attributeValue("length"))
                    : Integer.parseInt(subNode.attributeValue("compressLength"));

            if (data.length()<length){
                v_tag_temp = data ;
            }else{
                v_tag_temp = data.substring(0,length);
            }
            v_tag_temp1 = v_tag_temp ;
            value = v_tag_temp;
            name = subNode.attributeValue("metadataid")  ;
            log.info("field Name is : " + name + " field value is : " + value);
            map.put(name, value);
            data = replace(data,v_tag_temp1,"",1);
        }
    }
    public static void StringTagPack(String data, String fieldName, HashMap<String, Object> map, Element curNode) throws UnsupportedEncodingException {
        log.info("StringTagPack 原始值：" + data);

        String name = "" ;
        String usage = "" ;
        String v_tag_temp = "" ;
        String v_tag_temp1 = "" ;
        String value = "" ;

        if(null != data && data.length() != 0) {
            while (data.length() > 0) {

            }
        }

        Iterator itr = curNode.elementIterator();

        while (itr.hasNext()){
            Element subNode = (Element)itr.next();
            int length = subNode.attributeValue("compressLength") == null
                    ? Integer.parseInt(subNode.attributeValue("length"))
                    : Integer.parseInt(subNode.attributeValue("compressLength"));

            if (data.length()<length){
                v_tag_temp = data ;
            }else{
                v_tag_temp = data.substring(0,length);
            }
            v_tag_temp1 = v_tag_temp ;
            value = v_tag_temp;
            name = subNode.attributeValue("metadataid")  ;
            log.info("field Name is : " + name + " field value is : " + value);
            map.put(name, value);
            data = replace(data,v_tag_temp1,"",1);
        }
    }
    public static String toStringHex(String s) throws UnsupportedEncodingException {
        if(s.startsWith("0x")){
            s = s.substring(2);
        }
        byte[] baKeyword = new byte[s.length()/2] ;
        for(int i = 0; i <baKeyword.length ;i++){
            baKeyword[i] = (byte)(0xff & Integer.parseInt(s.substring(i*2,i*2+2),16));
        }
        s= new String(baKeyword , "GBK");
        return s ;
    }

    public static String replace(String text, String repl, String with, int max) {
        if (text != null && ! StringUtils.isEmpty(repl) && with != null && max != 0) {
            StringBuffer buf = new StringBuffer(text.length());
            int start = 0;
            boolean var6 = false;

            int end;
            while((end = text.indexOf(repl, start)) != -1) {
                buf.append(text.substring(start, end)).append(with);
                start = end + repl.length();
                --max;
                if (max == 0) {
                    break;
                }
            }

            buf.append(text.substring(start));
            return buf.toString();
        } else {
            return text;
        }
    }


}
